home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Pascal / Snippets / PNL Libraries / MyRequiredEventSupport.p < prev    next >
Encoding:
Text File  |  1996-12-20  |  5.0 KB  |  161 lines  |  [TEXT/CWIE]

  1. unit MyRequiredEventSupport;
  2.  
  3. interface
  4.  
  5.     uses
  6.         Types, Files, AppleEvents, MyTypes;
  7.  
  8.     var
  9.         required_event_with_option: boolean;
  10.         required_event_with_control: boolean;
  11.  
  12.     type
  13.         DocProc = function (var fs:FSSpec): OSErr;
  14.         NoDocProc = function: OSErr;
  15.     
  16.     procedure InitAppleEvents (DoOpenApplication:NoDocProc; DoOpenDocuments:DocProc; DoPrintDocuments: DocProc; DoQuitApplication: NoDocProc);
  17.  
  18. implementation
  19.  
  20.     uses
  21.         Processes, Events, Memory, EPPC, 
  22.         MyAEUtils, MyEvents, MyAssertions;
  23.         
  24.     var
  25.         gDoOpenApplication: NoDocProc;
  26.         gDoOpenDocuments: DocProc;
  27.         gDoPrintDocuments: DocProc;
  28.         gDoQuitApplication: NoDocProc;
  29.  
  30.     procedure GetModifiers(var event: AppleEvent);
  31.         var
  32.             err, junk: OSErr;
  33.             psn: ProcessSerialNumber;
  34.             pi: ProcessInfoRec;
  35.             dummy: Boolean;
  36.             er: EventRecord;
  37.             keywd: AEKeyword;
  38.             actualSize: Size;
  39.             attrdesc:AEDesc;
  40.             targrec: TargetID;
  41.     begin
  42.     { check if the event comes from the finder and honour the option/control keys }
  43.         required_event_with_control := false;
  44.         required_event_with_option := false;
  45.         AECreate( attrdesc );
  46.         err := AEGetAttributePtr(event, keyAddressAttr, keyProcessSerialNumber, keywd, @psn, SizeOf(psn), actualSize);
  47.         if err <> noErr then begin
  48.             err := AEGetAttributePtr(event, keyAddressAttr, typeTargetID, keywd, @targrec, SizeOf(targrec), actualSize);
  49.             if err = noErr then begin
  50.                 err := GetProcessSerialNumberFromPortName( targrec.name, psn );
  51.             end;
  52.         end;
  53.         if err = noErr then begin
  54.             pi.processInfoLength := sizeof(ProcessInfoRec);
  55.             pi.processName := nil;
  56.             pi.processAppSpec := nil;
  57.             err := GetProcessInformation(psn, pi);
  58.         end;
  59.         if (err = noErr) & (pi.processSignature = 'MACS') then begin
  60.             dummy := OSEventAvail(everyEvent, er);
  61.             required_event_with_option := EventHasOptionKey( er );
  62.             required_event_with_control := EventHasControlKey( er );
  63.         end;
  64.         junk := GetBooleanFromAERecord(event, keyOdocOption, required_event_with_option);
  65.         junk := GetBooleanFromAERecord(event, keyOdocControl, required_event_with_control);
  66.         AEDestroy( attrdesc );
  67.     end;
  68.     
  69.     function HandleNoDocs (var event, reply: AppleEvent; Doit:NoDocProc): OSErr;
  70.         var
  71.             err: OSErr;
  72.     begin
  73. {$unused(reply)}
  74.         GetModifiers(event);
  75.         err := GotRequiredParams(event);
  76.         if err = noErr then begin
  77.             err := Doit;
  78.         end;
  79.         HandleNoDocs := err;
  80.     end;
  81.  
  82.     function HandleDocs (var event, reply: AppleEvent; Doit:DocProc): OSErr;
  83.         var
  84.             myFSS: FSSpec;
  85.             docList: AEDescList;
  86.             index, itemsInList: longint;
  87.             typeCode: DescType;
  88.             err, oerr, junk: OSErr;
  89.             keywd: AEKeyword;
  90.             actualSize: Size;
  91.     begin
  92. {$unused(reply)}
  93.         GetModifiers(event);
  94.         err := AEGetParamDesc(event, keyDirectObject, typeAEList, docList);
  95.         if err = noErr then begin
  96.             junk := GotRequiredParams(event);
  97. (* now get each alias from the list (as an FSSSpec) and open the associated file. *)
  98.             err := AECountItems(docList, itemsInList);
  99.             if err = noErr then begin
  100.                 for index := 1 to itemsInList do begin
  101.                     oerr := AEGetNthPtr(docList, index, typeFSS, keywd, typeCode, @myFSS, sizeof(myFSS), actualSize);
  102.     (* coercion does alias->fsspec *)
  103.                     if oerr = noErr then begin
  104.                         Assert( Doit <> nil );
  105.                         oerr := Doit(myFSS);
  106.                     end;
  107.                 end;
  108.             end;
  109.             junk := AEDisposeDesc(docList);
  110.         end;
  111.         HandleDocs := err;
  112.     end;
  113.  
  114.     function HandleOpenApplication(var event, reply: AppleEvent; ignored: longint): OSErr;
  115.     begin
  116. {$unused(ignored)}
  117.         HandleOpenApplication := HandleNoDocs(event, reply, gDoOpenApplication);
  118.     end;
  119.  
  120.     function HandleOpenDocuments (var event, reply: AppleEvent; ignored: longint): OSErr;
  121.     begin
  122. {$unused(ignored)}
  123.         HandleOpenDocuments := HandleDocs(event, reply, gDoOpenDocuments);
  124.     end;
  125.     
  126.     function HandlePrintDocuments(var event, reply: AppleEvent; ignored: longint): OSErr;
  127.     begin
  128. {$unused(ignored)}
  129.         HandlePrintDocuments := HandleDocs(event, reply, gDoPrintDocuments);
  130.     end;
  131.     
  132.     function HandleQuitApplication(var event, reply: AppleEvent; ignored: longint): OSErr;
  133.     begin
  134. {$unused(ignored)}
  135.         HandleQuitApplication := HandleNoDocs(event, reply, gDoQuitApplication);
  136.     end;
  137.  
  138.     procedure InitAppleEvents (DoOpenApplication:NoDocProc; DoOpenDocuments:DocProc; DoPrintDocuments: DocProc; DoQuitApplication: NoDocProc);
  139.         var
  140.             junk: OSErr;
  141.     begin
  142.         gDoOpenApplication := DoOpenApplication;
  143.         gDoOpenDocuments := DoOpenDocuments;
  144.         gDoPrintDocuments := DoPrintDocuments;
  145.         gDoQuitApplication := DoQuitApplication;
  146.         if (@gDoOpenApplication <> nil) then begin
  147.             junk := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(HandleOpenApplication), 0, false);
  148.         end;
  149.         if (@gDoOpenDocuments <> nil) then begin
  150.             junk := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(HandleOpenDocuments), 0, false);
  151.         end;
  152.         if (@gDoPrintDocuments <> nil) then begin
  153.             junk := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc(HandlePrintDocuments), 0, false);
  154.         end;
  155.         if (@gDoQuitApplication <> nil) then begin
  156.             junk := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(HandleQuitApplication), 0, false);
  157.         end;
  158.     end;
  159.  
  160. end.
  161.